package com.team.owner.service.impl;

import com.team.owner.mapper.OwnerMapper;
import com.team.owner.model.*;
import com.team.owner.service.OwnerService;
import com.team.util.AllRecordsVo;
import com.team.util.ResultBuildVo;
import com.team.util.ResultVo;
import com.team.util.StatusShengJIEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by 李红历 on 2022/7/12.
 */
@Service
public class OwnerServiceImpl implements OwnerService{
    @Autowired
    private OwnerMapper ownerMapper;

    @Override
    public ResultVo queryPage(OwnerQueryVo ownerQueryVo) {
        List<OwnerVo> ownerVoList = ownerMapper.queryData(ownerQueryVo);
        for(OwnerVo ownerVo:ownerVoList){
            Integer status =ownerVo.getStatus()?1:0;
            ownerVo.setStatusName(StatusShengJIEnum.queryNameById(status));
        }
        int count = ownerMapper.queryCount(ownerQueryVo);
        AllRecordsVo allRecordsVo =new AllRecordsVo();
        allRecordsVo.setDataList(ownerVoList);
        allRecordsVo.setPageIndex(ownerQueryVo.getPageIndex());
        allRecordsVo.setPageSize(ownerQueryVo.getPageSize());
        allRecordsVo.setTotalNumber(count);
        allRecordsVo.setTotalPage(allRecordsVo.countPage(count));
        return ResultBuildVo.success(allRecordsVo);
    }

    @Override
    public ResultVo queryware(QueryWareVo queryWareVo) {
        List<QueryWareVo> queryWareVoList = ownerMapper.queryDataWare(queryWareVo);
        for (QueryWareVo queryWareVo1 : queryWareVoList){
            Integer status =queryWareVo1.getStatus()?1:0;
            queryWareVo1.setStatusName(StatusShengJIEnum.queryNameById(status));
        }
        return ResultBuildVo.success(queryWareVoList);
    }

    @Override
    public ResultVo queryareaByCode(List<CodeVo> stockRoomCodeLists) {
        if (stockRoomCodeLists == null ||stockRoomCodeLists.size() == 0) {
            ResultBuildVo.error("没有选择任何库房");
        }
        ListVo listVo = new ListVo();
        listVo.setStockRoomCodeLists(stockRoomCodeLists);
        List<QueryAreaVo> queryAreaVoList = ownerMapper.queryareaByCode(listVo);
        for (QueryAreaVo queryAreaVo:queryAreaVoList){
            Integer status =queryAreaVo.getStatus()?1:0;
            queryAreaVo.setStatusName(StatusShengJIEnum.queryNameById(status));
        }
        return ResultBuildVo.success(queryAreaVoList);
    }

    @Override
    public ResultVo queryseatByCode(List<CodeVo> wareAreaCodeLists) {
        if (wareAreaCodeLists == null ||wareAreaCodeLists.size() == 0) {
            ResultBuildVo.error("没有选择任何库区");
        }
        ListVo listVo = new ListVo();
        listVo.setWareAreaCodeLists(wareAreaCodeLists);
        List<QuerySeatVo> querySeatVoList = ownerMapper.queryseatByCode(listVo);
        for (QuerySeatVo querySeatVo:querySeatVoList){
            Integer status =querySeatVo.getStatus()?1:0;
            querySeatVo.setStatusName(StatusShengJIEnum.queryNameById(status));
        }
        return ResultBuildVo.success(querySeatVoList);
    }

    @Override
    public ResultVo querylegalPersonCode(String legalPersonCode) {
        Map<String,List> returnMap = new HashMap<>();
        //需要去重
        List<QueryWareVo> queryWareVoList = new ArrayList<>();
        List<QueryAreaVo> queryAreaVoList = new ArrayList<>();

        List<QuerySeatVo> querySeatVoList = new ArrayList<>();
        List<QueryVo> queryVos = ownerMapper.querylegalPersonCode(legalPersonCode);
        for (QueryVo queryVo : queryVos){
            //库位
            QuerySeatVo querySeatVo = new QuerySeatVo();
            Integer i1 =queryVo.getSstatus()?1:0;
            queryVo.setSstatusName(StatusShengJIEnum.queryNameById(i1));
            querySeatVo.setStatus(queryVo.getSstatus());
            querySeatVo.setStatusName(queryVo.getSstatusName());
            querySeatVo.setId(queryVo.getSid());
            querySeatVo.setCode(queryVo.getScode());
            querySeatVo.setName(queryVo.getSname());
            querySeatVo.setRemark(queryVo.getSremark());
            querySeatVo.setAreaCode(queryVo.getSareaCode());
            querySeatVoList.add(querySeatVo);
            //库区
            QueryAreaVo queryAreaVo = new QueryAreaVo();
            Integer i2 =queryVo.getAstatus()?1:0;
            queryVo.setAstatusName(StatusShengJIEnum.queryNameById(i2));
            queryAreaVo.setStatus(queryVo.getAstatus());
            queryAreaVo.setStatusName(queryVo.getAstatusName());
            queryAreaVo.setId(queryVo.getAid());
            queryAreaVo.setCode(queryVo.getAcode());
            queryAreaVo.setName(queryVo.getAname());
            queryAreaVo.setRemark(queryVo.getAremark());
            queryAreaVo.setWareHouseCode(queryVo.getAwareHouseCode());
            queryAreaVoList.add(queryAreaVo);
            //库房
            QueryWareVo queryWareVo = new QueryWareVo();
            Integer i3 =queryVo.getWstatus()?1:0;
            queryVo.setWstatusName(StatusShengJIEnum.queryNameById(i3));
            queryWareVo.setStatus(queryVo.getWstatus());
            queryWareVo.setStatusName(queryVo.getWstatusName());
            queryWareVo.setId(queryVo.getWid());
            queryWareVo.setCode(queryVo.getWcode());
            queryWareVo.setName(queryVo.getWname());
            queryWareVo.setRemark(queryVo.getWremark());
            queryWareVo.setDetailAddress(queryVo.getWdetailAddress());
            queryWareVoList.add(queryWareVo);
        }
        //两种算法去重
        //1.双重for循环
        for (int i = 0;i<queryWareVoList.size()-1;i++){
            for (int j=queryWareVoList.size()-1;j>i;j--){
                if (queryWareVoList.get(j).equals(queryWareVoList.get(i))){
                    queryWareVoList.remove(j);
                }
            }
        }
        //2.set去重
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = queryAreaVoList.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (set.add(element))
                newList.add(element);
        }
        queryAreaVoList.clear();
        queryAreaVoList.addAll(newList);

        returnMap.put("seat",querySeatVoList);
        returnMap.put("area",queryAreaVoList);
        returnMap.put("ware",queryWareVoList);
        return ResultBuildVo.success(returnMap);
    }

    @Override
    public ResultVo update(UpdateVo updateVo) {
        String legalPersonCode = updateVo.getLegalPersonCode();
        List<String> codeList = updateVo.getCodeList();
        for (String code:codeList){
            UpdateVo updateVo1 = new UpdateVo();
            updateVo1.setLegalPersonCode(legalPersonCode);
            updateVo1.setCode(code);
            ownerMapper.update(updateVo1);
        }
        return ResultBuildVo.success();
    }
}
